package com.levi.mall.controller;

import com.github.pagehelper.PageInfo;
import com.levi.form.ShippingForm;
import com.levi.mall.service.ShippingService;
import com.levi.pojo.Shipping;
import com.levi.pojo.User;
import com.levi.vo.ResponseVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author shentong
 * @since 2022/4/4 10:11 PM
 */
@RestController
@RequestMapping("/shippings")
public class ShippingController extends BaseController{

    @Autowired
    private ShippingService shippingService;

    @PostMapping
    @ApiOperation("添加收货地址")
    public ResponseVo<Map<String,Integer>> add(@RequestBody @Valid ShippingForm form,
                                               HttpSession session) {
        User user = getCurrentUser(session);
        return shippingService.add(user.getId(), form);
    }

    @DeleteMapping("/{shippingId}")
    @ApiOperation("删除收货地址")
    public ResponseVo delete(@PathVariable("shippingId") Integer shippingId, HttpSession session) {
        User user = getCurrentUser(session);
        return shippingService.delete(user.getId(), shippingId);
    }

    @PutMapping("/{shippingId}")
    @ApiOperation("修改收货地址")
    public ResponseVo update(@PathVariable("shippingId") Integer shippingId, HttpSession session,
                             @RequestBody @Valid ShippingForm form) {
        User user = getCurrentUser(session);
        return shippingService.update(user.getId(), shippingId, form);
    }

    @GetMapping
    @ApiOperation("收货地址分页")
    public ResponseVo<PageInfo<Shipping>> list(@ApiParam("第几页") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                               @ApiParam("每页多少条数据") @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                               HttpSession session) {
        User user = getCurrentUser(session);
        return shippingService.list(user.getId(), pageNum, pageSize);
    }
}
